#include "VisualManager.h"


VisualManager::VisualManager(int numberOfLayers)
{

	pixelSize = 1;
	width = ofGetWidth();
	height = ofGetHeight();

	bits = 4;
	this->numberOfLayers = numberOfLayers;
	buffer = new ofImage();
	buffer->allocate(width,height,OF_IMAGE_COLOR_ALPHA);

	bufferPixels = buffer->getPixels();
	for(int i =0;i<numberOfLayers;i++)
	{

		ofxFBOTexture* fbo = new ofxFBOTexture();
		fbo->allocate(width, height, GL_RGBA);

		buffers.push_back(fbo);
		//layers.push_back((unsigned char*)fbo->getPixels());
	}
	printf("buffer created");

	frontImage = new ofImage();
	frontImage->loadImage("data/front.JPG");
	frontImage->setImageType(OF_IMAGE_COLOR_ALPHA);
	frontPixels = frontImage->getPixels();

	backImage = new ofImage();

	backImage->loadImage("data/back.png");
	backImage->setImageType(OF_IMAGE_COLOR_ALPHA);
	backPixels = backImage->getPixels();

	frontVideo.loadMovie("data/front.mov");
    frontVideo.play();
	//frontVideo.setPosition(0.5f);
	//frontVideo.setSpeed(0.3f);

}


VisualManager::~VisualManager(void)
{
}

void VisualManager::beginLayerBuffer(int layerIndex)
{

	ofSetColor(0xffffffff);
	buffers[layerIndex]->clear(0,0,0,0);
	buffers[layerIndex]->begin();

}

void VisualManager::endLayerBuffer(int layerIndex)
{

	buffers[layerIndex]->end();


	//layers[layerIndex]=(unsigned char*)buffers[layerIndex]->getPixels();
    //blur.endRender();

	//layers[layerIndex].update();
}

void VisualManager::draw(int layerIndex)
{
    ofSetColor(0xffffffff);
	buffers[layerIndex]->draw(0,0);

    //frontImage->draw(0,0,width,height);
    //frontVideo.draw(0,0,width,height);
    //buffer->draw(0,0);
    return;
   // frontImage->draw(0,0);

    //ofSetColor(0xffffffff);
    //frontImage->draw(0,0);
    return;


	// = new unsigned char[width*height*bits];

	/*

	int i,iR,add,fuckAdd,iP;
	int alpha,minadd;

		for (int j = 0; j < width; j+=pixelSize)
		{
		for (int k = 0; k < height; k+=pixelSize)
		{
			i = (k+pixelSize/2)*width+j+pixelSize/2;
			iR = (height-k-pixelSize/2)*width+j+pixelSize/2; //reversed y


			{
			bufferPixels[i*bits] = frontPixels[i*bits];
			bufferPixels[i*bits+1] = frontPixels[i*bits+1];
			bufferPixels[i*bits+2] = frontPixels[i*bits+2];
			bufferPixels[i*bits+3] = frontPixels[i*bits+3];
			}

			if(layers[2][iR*bits+3]>0)
			{
			alpha = (layers[2][iR*bits+3]/255.0f);

			add = (-rand()%255)*alpha;
			fuckAdd = rand()%(int)max(1,(int)(layers[2][iR*bits]));
			bufferPixels[i*bits] = max(0,min(255,(int)(bufferPixels[i*bits]*(1-alpha)+backPixels[i*bits]*alpha))+fuckAdd);
			bufferPixels[i*bits+1] =max(0,min(255,(int)(bufferPixels[i*bits+1]*(1-alpha)+backPixels[i*bits+1]*alpha))+fuckAdd);
			bufferPixels[i*bits+2] = max(0,min(255,(int)(bufferPixels[i*bits+2]*(1-alpha)+backPixels[i*bits+2]*alpha))+fuckAdd);
			//bufferPixels[i*bits+3] = layers[2][iR*bits+3];
			}



			if(layers[1][iR*bits+3]>0)
			{

			alpha = (layers[1][iR*bits+3]/255.0f);

			add = (-rand()%40);
			minadd = (10-rand()%20);
			fuckAdd = rand()%(int)fmax(1,(layers[1][iR*bits]));
			bufferPixels[i*bits] = max(0,min(255,(int)(bufferPixels[i*bits]*(1-alpha/3)+add*alpha+minadd))+fuckAdd);
			bufferPixels[i*bits+1] =max(0,min(255,(int)(bufferPixels[i*bits+1]*(1-alpha/3)+add*alpha+minadd))+fuckAdd);
			bufferPixels[i*bits+2] = max(0,min(255,(int)(bufferPixels[i*bits+2]*(1-alpha/3)+add*alpha+minadd))+fuckAdd);
			//bufferPixels[i*bits+3] = min(255,layers[1][i*bits+3]+100);
			}

			if(k+pixelSize > height || j+pixelSize > width)
				continue;

			for(int l=0;l<pixelSize;l++)
			{
				for(int m=0;m<pixelSize;m++)
				{
					iP = (k+m)*width+j+l;
					bufferPixels[iP*bits] = bufferPixels[i*bits];
					bufferPixels[iP*bits+1] = bufferPixels[i*bits+1];
					bufferPixels[iP*bits+2] = bufferPixels[i*bits+2];
					bufferPixels[iP*bits+3] = bufferPixels[i*bits+3];
				}
			}

		}
		}


		buffer->update();

		ofSetColor(0xffffff);
		buffer->draw(0,0);

		*/

}
void VisualManager::update()
{
	if(startPixelBeatTime+growPixelBeatTime>ofGetElapsedTimef())
	{
		//grow
		pixelSize =fmax(1,((ofGetElapsedTimef()-startPixelBeatTime)/growPixelBeatTime)*targetPixelSize);
		//pixelSize = targetPixelSize;
	}
	else
	if(startPixelBeatTime+growPixelBeatTime+shrinkPixelBeatTime>ofGetElapsedTimef())
	{
		//shrink
		pixelSize =fmax(1,(1-(ofGetElapsedTimef()-startPixelBeatTime-growPixelBeatTime)/shrinkPixelBeatTime)*targetPixelSize);
		//pixelSize = 1;

	}
	else
		pixelSize = 1;

    frontVideo.idleMovie();

}

void VisualManager::beatPixelSize(float timeIn,float timeOut, int size)
{
	printf("beat pixels started \n");
	startPixelBeatTime = ofGetElapsedTimef();
	growPixelBeatTime = timeIn;
	shrinkPixelBeatTime = timeOut;
	targetPixelSize = size;

}
